<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\BrandModel;
use Admin\Model\VersionModel;
use Admin\Model\OptionCategoryModel;
use Admin\Model\OptionModel;

/**
* Created by hanquan.
* User Adminstrator
* Date 2017/3/26
* Time 22:05
* AjaxController Ajax请求类
*/
class AjaxController extends UserBaseController
{
	
	function __construct()
	{
		parent::__construct();
		if(!$this->uid){

		}
	}

	/**
     * 获取品牌机型
     */
	public function get_brand_version(){

		$Brand =new BrandModel();
		$Version =new VersionModel();
		$brand_version = $Brand->get_brand(); 
		foreach ( $brand_version as $key => $brand) {
			$brand_version[$key]['version'] = $Version->get_version($brand['bid']);
		}
		returnMobileJson(200,'获取成功',$brand_version);
	}

	/**
     * 添加品牌
     */
	public function add_brand(){
		$name = $_POST['name'];
		if(empty($name)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('b_name' => $name, 'b_date' => date('Y-m-d H:i:s'));
		$Brand = new BrandModel();
		if($Brand->set_brand($data)){
			returnMobileJson(200,'添加成功');
		}else{
			returnMobileJson(202,'添加失败');
		}
	}

	/**
	 * 编辑品牌
	 */
	public function edit_brand(){
		$bid = $_POST['bid'];
		$name = $_POST['name'];
		if(empty($bid) || empty($name)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('b_name' => $name, 'b_date' => date('Y-m-d H:i:s'));
		$Brand = new BrandModel();
		if($Brand->set_brand($data,$bid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
	 * 删除品牌
	 */
	public function del_brand(){
		$bid = $_POST['bid'];
		if(empty($bid)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('b_status' => 0);
		$Brand = new BrandModel();
		if($Brand->set_brand($data,$bid)){
			returnMobileJson(200,'删除成功');
		}else{
			returnMobileJson(202,'删除失败');
		}
	}

	/**
     * 添加机型
     */
	public function add_version(){
		$name = $_POST['name'];
		$imgurl = $_POST['imgurl'];
		$bid = $_POST['bid'];
		if(empty($bid) || empty($name) || empty($imgurl)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('v_imgurl' => $imgurl, 'v_name' => $name, 'v_date' => date('Y-m-d H:i:s'), 'bid' => $bid);
		$Version = new VersionModel();
		if($Version->set_version($data)){
			returnMobileJson(200,'添加成功');
		}else{
			returnMobileJson(202,'添加失败');
		}
	}

	/**
     * 编辑机型
     */
	public function edit_version(){
		$vid = $_POST['vid'];
		$name = $_POST['name'];
		$imgurl = $_POST['imgurl'];
		if(empty($vid) || empty($name) || empty($imgurl)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('v_imgurl' => $imgurl, 'v_name' => $name, 'v_date' => date('Y-m-d H:i:s'));
		$Version = new VersionModel();
		if($Version->set_version($data,$vid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
     * 删除机型
     */
	public function del_version(){
		$vid = $_POST['vid'];
		if(empty($vid)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('v_status' => 0);
		$Version = new VersionModel();
		if($Version->set_version($data,$vid)){
			returnMobileJson(200,'删除成功');
		}else{
			returnMobileJson(202,'删除失败');
		}
	}

	/**
     * 设置最高回收价
     */
	public function set_max_price(){
		$vid = $_POST['vid'];
		$max_price = $_POST['max_price'];
		if(empty($vid) || empty($max_price)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('v_max_price' => $max_price, 'v_date' => date('Y-m-d H:i:s'));
		$Version = new VersionModel();
		if($Version->set_version($data,$vid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
     * 设置最高低回收价
     */
	public function set_min_price(){
		$vid = $_POST['vid'];
		$min_price = $_POST['min_price'];
		if(empty($vid) ||  empty($min_price)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('v_min_price' => $min_price, 'v_date' => date('Y-m-d H:i:s'));
		$Version = new VersionModel();
		if($Version->set_version($data,$vid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
     * 获取选项分类
     */
	public function get_option_category(){
		$vid = $_POST['vid'];
		if(empty($vid)){
			returnMobileJson(201,'参数错误');
		}
		$OptionCategory =new OptionCategoryModel();
		$Option =new OptionModel();
		$Version = new VersionModel();
		$recycle_cost = $Version->get_recycle_cost($vid);

		$option_categorys = $OptionCategory->get_option_category($vid);
		foreach ( $option_categorys as $key => $option_category) {
			$option_categorys[$key]['option'] = $Option->get_option($option_category['cid']);
		}

		$data['recycle_cost'] = $recycle_cost;
		$data['option_category'] = $option_categorys;
		returnMobileJson(200,'获取成功',$data);
	}

	/**
     * 添加选项分类
     */
	public function add_option_category(){
		$vid = $_POST['vid'];
		$name = $_POST['name'];
		$require = $_POST['require'];
		$radio = $_POST['radio'];
		if(empty($vid) || empty($name) || !isset($require) || !isset($radio)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('c_name' => $name, 'c_require' => $require, 'c_radio' => $radio, 'c_date' => date('Y-m-d H:i:s'), 'vid' => $vid);
		$OptionCategory = new OptionCategoryModel();
		if($OptionCategory->set_option_category($data)){
			returnMobileJson(200,'添加成功');
		}else{
			returnMobileJson(202,'添加失败');
		}
	}

	/**
     * 编辑选项分类
     */
	public function edit_option_category(){
		$cid = $_POST['cid'];
		$require = $_POST['require'];
		$radio = $_POST['radio'];
		if(empty($cid) || !isset($_POST['require']) || !isset($_POST['radio'])){
			returnMobileJson(201,'参数错误');
		}
		$data = array('c_require' => $require, 'c_radio' => $radio, 'c_date' => date('Y-m-d H:i:s'));
		$OptionCategory = new OptionCategoryModel();
		if($OptionCategory->set_option_category($data,$cid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
     * 删除选项分类
     */
	public function del_option_category(){
		$cid = $_POST['cid'];
		if(empty($cid)){
			returnMobileJson(201,'参数错误');
		}
		$OptionCategory = new OptionCategoryModel();
		if($OptionCategory->del_option_category($cid)){
			returnMobileJson(200,'删除成功');
		}else{
			returnMobileJson(202,'删除失败');
		}
	}

	/**
     * 添加选项
     */
	public function add_option(){
		$cid = $_POST['cid'];
		$label = $_POST['label'];
		$price = $_POST['price'];
		if(empty($cid) || empty($label)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('o_label' => $label, 'o_price' => $price, 'o_date' => date('Y-m-d H:i:s'), 'cid' => $cid);
		$Option = new OptionModel();
		if($Option->set_option($data)){
			returnMobileJson(200,'添加成功');
		}else{
			returnMobileJson(202,'添加失败');
		}
	}

	/**
     * 编辑选项
     */
	public function edit_option(){
		$oid = $_POST['oid'];
		$label = $_POST['label'];
		$price = $_POST['price'];
		if(empty($oid) || empty($label)){
			returnMobileJson(201,'参数错误');
		}
		$data = array('o_label' => $label, 'o_price' => $price, 'o_date' => date('Y-m-d H:i:s'));
		$Option = new OptionModel();
		if($Option->set_option($data,$oid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
     * 删除选项
     */
	public function del_option(){
		$oid = $_POST['oid'];
		if(empty($oid)){
			returnMobileJson(201,'参数错误');
		}
		$Option = new OptionModel();
		if($Option->del_option($oid)){
			returnMobileJson(200,'删除成功');
		}else{
			returnMobileJson(202,'删除失败');
		}
	}

}